package com.fyy.shardingjdbc.dao;

import com.fyy.shardingjdbc.model.ProductDescript;
import com.fyy.shardingjdbc.model.ProductInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ProductDao {

    @Insert("INSERT INTO t_product_info " +
            "(store_id, name, chandi, price) values (" +
            "#{storeId}," +
            "#{name}," +
            "#{chandi}," +
            "#{price}" +
            ")")
    @Options(useGeneratedKeys = true)
    int insertProduct(ProductInfo productInfo);

    @Insert("INSERT INTO t_product_descript " +
            "(product_id, miaoshu, store_id) values (" +
            "#{productId}," +
            "#{miaoshu}," +
            "#{storeId}" +
            ")")
    int insertProductDes(ProductDescript productDescript);

    @Select("select p.*,d.miaoshu,r.name diyu " +
            "from t_product_info p " +
            "left join t_product_descript d ON d.product_id = p.id " +
            "LEFT JOIN t_region r ON r.code = p.chandi " +
            "ORDER BY p.price DESC " +
            "LIMIT #{start},#{pageSize}")
    List<ProductInfo> queryProduct(@Param("start")Integer start
            ,@Param("pageSize")Integer pageSize);

    @Select("select count(1) from t_product_info")
    int getProductTotal();

    @Select("select * from t_product_info group by store_id")
    List<ProductInfo> queryProduct();

}
